/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataanalysis;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import weka.classifiers.Classifier;
import weka.core.Attribute;
import weka.core.Instances;

/**
 *
 * @author Administrator
 */
public class Model implements Serializable {

    private final Classifier m_classifier;
    private final Instances m_header;

    public Model(Classifier classifier, Instances header) {
        m_classifier = classifier;
        m_header = header;
    }

    public Classifier getClassifier() {
        return m_classifier;
    }

    public Instances getDataHeader() {
        return m_header;
    }

    public Vector<String> getHeaderVector() {
        Vector<String> result = new Vector();
        for (int i = 0; i < m_header.numAttributes(); i++) {
            result.add(m_header.attribute(i).toString().substring(11));
        }
        return result;
    }

    public TreeModel getHeaderStructure() {
        DefaultMutableTreeNode mutableTreenode = new DefaultMutableTreeNode("模型数据结构");
        for (int i = 0; i < m_header.numAttributes(); i++) {
            Attribute att = m_header.attribute(i);
            String text = att.name() + " : " + this.getAttTypeString(att.type());
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(text);
            mutableTreenode.add(node);
            if (att.type() == Attribute.NOMINAL) {
                Enumeration enm = att.enumerateValues();
                while (enm.hasMoreElements()) {
                    node.add(new DefaultMutableTreeNode(enm.nextElement().toString()));
                }
            }
        }
        return new DefaultTreeModel(mutableTreenode);
    }

    @Override
    public String toString() {
        return m_classifier.toString().substring(1);
    }

    public String getType() {
        String str1 = m_classifier.getClass().getName();
        String str2 = m_header.relationName();
        int begin1 = str1.lastIndexOf('.');
        if (begin1 != -1) {
            str1 = str1.substring(str1.lastIndexOf('.') + 1);
        }
        int end2 = str2.indexOf('-');
        if (end2 != -1) {
            str2 = str2.substring(0, str2.indexOf('-'));
        }
        return str1 + "_" + str2;
    }

    private String getAttTypeString(int type) {
        switch (type) {
            case Attribute.DATE:
                return "DATE";
            case Attribute.NOMINAL:
                return "NOMINAL";
            case Attribute.NUMERIC:
                return "NUMERIC";
            case Attribute.RELATIONAL:
                return "RELATIONAL";
            case Attribute.STRING:
                return "STRING";
            default:
                return null;
        }
    }
}
